// pages/home/home.js
import { Banner } from '../../model/banner';
import { Activity } from '../../model/activity';
import { UserAuth } from '../../model/user-auth';
import { User } from '../../model/user';
const appInst =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeColor: appInst.globalData.changeColor,
    fontColor: appInst.globalData.fontColor,
    titleColor: appInst.globalData.titleColor,
    moreColor: appInst.globalData.moreColor,
    whiteColor: appInst.globalData.whiteColor,
    banner: [],
    activity: [],
    user_auth: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
        title: '加载中',
      });
      wx.getLocation({
        type: 'wgs84',
        success(res){
          console.log(res);
        }
      });
      if(options.openid) {
          this.createInvite(options.openid);
      }
      this.getBannerList();
  },
  async createInvite(openid){
    await User.createInvite({openid:openid})
  },
  /**
   * 获取banner图
   */
  async getBannerList() {
    const res = await Banner.getDataList( 1)
    this.setData({
      banner: res.items,
    })
    this.getRecommendActivity();
  },
  /**
   * 获取推荐活动
   */
  async getRecommendActivity() {
    const res = await Activity.getRecommendActivity( {});
    this.setData({
      activity: res,
    })
    this.getUserAuthRecommendList()
  },
  /**
   * 获取用户认证信息列表
   */
  async getUserAuthRecommendList() {
    const res = await UserAuth.getRecommendDataList({});
    this.setData({
      user_auth: res,
    })
    wx.hideLoading();
  },

  /**
   * 打开活动页面
   */
  openActivity() {
    wx.switchTab({
      url: '../activity/activity',
    })
  },
  /**
   * 打开活动详情
   */
  openActivityInfo(e) {
    appInst.activityLinkTo(e);
  },
  /**
   * 打开上墙页面
   */
  openQiang() {
    wx.switchTab({
      url: '../blind_date/blind_date',
    })
  },
  openQiangInfo(e){
    wx.navigateTo({
      url: `../blind_date/details?id=${e.currentTarget.dataset.id}`,
    })
  },
  /**
   * 打开banner连接
   */
  openBanner(e) {
    appInst.bannerLinkTo(e);
  }
})